package com.zh.work.controller.platform.channel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.channel.QuickRouteMapper;
import com.zh.work.entity.channel.QrCodeRouteMerchEntity;
import com.zh.work.entity.channel.QuickRouteEntity;
import com.zh.work.service.platform.channel.QuickRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 快捷路由管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/06
 * @Version: 1.0.1
 */
@Api(value = "快捷路由管理控制器", produces = "application/json", tags = {"快捷路由管理控制器"})
@RestController
@RequestMapping("/quickRoute")
public class QuickRouteController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/06
     */
    @Autowired
    private QuickRouteMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/06
     */
    @Autowired
    private QuickRouteService service;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/06
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增一条记录", tags = {"putAddQuickRoute"}, notes = "新增一条记录")
    @PutMapping("/putAddQuickRoute")
    @ResponseBody
    public JSONResult<Object> putAddQuickRoute(@ApiParam(value = "快捷路由实体类", name = "quickRouteEntity")
                                               @RequestBody QuickRouteEntity quickRouteEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putAddQuickRoute(quickRouteEntity);
            if (i > 0) {
                // 返回新增主键
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1)
                    jsonResult.setMessage("新增快捷路由规则失败!");
                else if (i == -2)
                    jsonResult.setMessage("新增快捷路由商户失败!");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModQuickRouteById"}, notes = "通过id修改信息")
    @PutMapping("/putModQuickRouteById")
    @ResponseBody
    public JSONResult<Object> putModQuickRouteById(@ApiParam(value = "快捷路由实体类", name = "quickRouteEntity")
                                                   @RequestBody QuickRouteEntity quickRouteEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.putModQuickRouteById(quickRouteEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1)
                    jsonResult.setMessage("删除快捷路由商户失败!");
                else if (i == -2)
                    jsonResult.setMessage("新增快捷路由商户失败!");
                else
                    jsonResult.setMessage("修改快捷路由失败!");
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteQuickRouteById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteQuickRouteById")
    public JSONResult<Object> deleteQuickRouteById(@ApiParam(value = "快捷路由实体类", name = "quickRouteEntity") QuickRouteEntity quickRouteEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (quickRouteEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteQuickRouteBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteQuickRouteBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteQuickRouteBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestParam(value = "ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getQuickRouteById"}, notes = "根据id查询详细信息")
    @GetMapping("/getQuickRouteById")
    public JSONResult<QuickRouteEntity> getQuickRouteById(@ApiParam(value = "快捷路由实体类", name = "quickRouteEntity") QuickRouteEntity quickRouteEntity) {
        JSONResult<QuickRouteEntity> jsonResult = new JSONResult<>();
        try {
            QuickRouteEntity result = this.service.getById(quickRouteEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesQuickRoutes"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesQuickRoutes")
    public JSONResult<List<QuickRouteEntity>> getPagesQuickRoutes(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "快捷路由实体类", name = "quickRouteEntity") QuickRouteEntity quickRouteEntity) {
        JSONResult<List<QuickRouteEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<QuickRouteEntity> result = this.service.selectQuickRoutePages(new Page<>(pageNo, pageSize), quickRouteEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改快捷路由状态", tags = {"putModQuickRouteStatus"}, notes = "修改快捷路由状态")
    @PutMapping("/putModQuickRouteStatus")
    @ResponseBody
    public JSONResult<Object> putModQuickRouteStatus(@ApiParam(value = "快捷路由实体类", name = "quickRouteEntity")
                                                             @RequestBody QuickRouteEntity quickRouteEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = this.mapper.updateById(quickRouteEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询指定快捷路由商户", tags = {"getQuickMerchantByNo"}, notes = "查询指定快捷路由商户")
    @GetMapping("/getQuickMerchantByNo")
    public JSONResult< List<QrCodeRouteMerchEntity>> getQuickMerchantByNo(@ApiParam(value = "快捷路由ID", name = "id") Long id) {
        JSONResult< List<QrCodeRouteMerchEntity>> jsonResult = new JSONResult<>();
        try {
            List<QrCodeRouteMerchEntity> list = this.mapper.getQuickMerchantByNo(id);
            jsonResult.setCode(200);
            jsonResult.setMessage("查询完成");
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }
}
